#include "Users.h"
#include "BbsControlData.h"
#include <string.h>
#include <stdio.h>
#include <device.h>
#include <unistd.h>
#include <conio.h>

void initUser()
{
	memset((void*)&_currentUser, 0, sizeof(user_t));
}

static userIndexRec_t current;

userIndexRec_t* findUserIndexRecByNumber(FILE *file, int usernumber)
{
	while(fread(&current, sizeof(userIndexRec_t), 1, file) == 1)
	{
		if(current.usernumber == usernumber) 
		{
			return &current;
		}
	}

	return NULL;
}

userIndexRec_t* findUserIndexRecByName(FILE *file, const char* username)
{
	while(fread(&current, sizeof(userIndexRec_t), 1, file) == 1)
	{
		if(strcmp(current.username, username) == 0) 
		{
			return &current;
		}
	}

	return NULL;
}

int loadUserByNumber(int userNumber, user_t *user)
{
	static FILE *file;
	static char oldDirectory[17];
	static char directory[17];
	static char filename[17];
	static int result;
	
	getcwd(oldDirectory, 17);
	getdevicedir(bcd.usersDirectory.device, directory, 17);
	chdir(directory);

	result = 0;

	sprintf(filename, "user.%05u", userNumber);

	file = fopen(filename, "r");

	if(file != NULL)
	{
		if(fread((void*)user, sizeof(user_t), 1, file) == 1)
		{
			result = userNumber;
		}
	}

	fclose(file);

	chdir(oldDirectory);

	return result;
}

int loadUserByName(char *userName, user_t *user)
{
	static FILE *file;
	static char oldDirectory[17];
	static char directory[17];
	static userIndexRec_t *rec;
	static int result;

	getcwd(oldDirectory, 17);
	getdevicedir(bcd.usersDirectory.device, directory, 17);
	chdir(directory);

	result = 0;

	file = fopen("userindex", "r");
	
	if(file != NULL)
	{
		rec = findUserIndexRecByName(file, userName);
		
		if(rec != NULL)
		{
			result = loadUserByNumber(rec->usernumber, user);
		}
	}

	fclose(file);

	chdir(oldDirectory);

	return result;
}

int writeUserIndexRecord(FILE *file, userIndexRec_t *record)
{
	static int r;
	if((r = fwrite(record, sizeof(userIndexRec_t), 1, file)) == 1)
	{
		return record->usernumber;
	}
	return 0;
}

int saveUser(user_t *user)
{
	static FILE *file, *newFile;
	static char filename[21];
	static char newname[21];
	static char oldDirectory[17];
	static char directory[17];
	static userIndexRec_t rr;
	static userIndexRec_t *rec;
	static user_t usr;
	int result = 0, counter = 0, target = 0;

	getcwd(oldDirectory, 17);
	getdevicedir(bcd.usersDirectory.device, directory, 17);
	chdir(directory);

	loadUserByName(user->username, &usr);

	if(usr.usernumber > 0 && usr.usernumber != user->usernumber)
	{
		return -1;
	}

	sprintf(filename, "%d:user.%05d", bcd.usersDirectory.drive, user->usernumber);

	file = fopen(filename, "w");

	fwrite(user, sizeof(user_t), 1, file);

	fclose(file);

	sprintf(filename, "%d:userindex", bcd.usersDirectory.drive);

	file = fopen(filename, "r");
	
	rec = &rr;

	rec->usernumber = user->usernumber;
	strcpy(rec->username, user->username);

	if(file != NULL)
	{
		if((rec = findUserIndexRecByNumber(file, user->usernumber)) == NULL)
		{
			fclose(file);
			file = fopen(filename, "a");
			result = writeUserIndexRecord(file, &rr);
		}
		else
		{
			fclose(file);

			sprintf(newname, "%d:userindex.bak", bcd.usersDirectory.drive);

			textcolor(COLOR_LIGHTBLUE);

			rename(filename, newname);

			file = fopen(newname, "r");
			newFile = fopen(filename, "w");

			while(fread(rec, sizeof(userIndexRec_t), 1, file) == 1)
			{
				if(rec->usernumber == user->usernumber)
				{
					strcpy(rec->username, user->username);
				}

				writeUserIndexRecord(newFile, rec);
			}

			fclose(newFile);

			result = user->usernumber;
		}
	}
	else
	{
		fclose(file);

		file = fopen(filename, "w");

		result = writeUserIndexRecord(file, rec);
	}

	fclose(file);

	chdir(oldDirectory);

	return result;
}

int getNextUserNumber(void)
{
	static FILE *file;
	static char oldDirectory[17];
	static char directory[17];
	static char filename[21];
	static userIndexRec_t rec;
	static int result, r;

	getcwd(oldDirectory, 17);
	getdevicedir(bcd.usersDirectory.device, directory, 17);
	chdir(directory);

	result = 0;

	sprintf(filename, "%d:userindex", bcd.usersDirectory.drive);
	file = fopen(filename, "r");
	
	if(file != NULL)
	{
		while((r = fread(&rec, sizeof(userIndexRec_t), 1, file)) == 1)
		{
			result = rec.usernumber + 1;
		}
	}
	else
	{
		result = 1;
	}

	fclose(file);

	chdir(oldDirectory);

	return result;
}